package com.itbaiwei.unitday01.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

/**
 * Class: MyListView
 * Description:
 * Author: haijun.xu
 * Date: 2025/5/28 15:10
 */
public class MyListView extends ListView {
    private float lastX,lastY;
    public MyListView(Context context) {
        super(context);
    }
    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        float x= ev.getX();
        float y=ev.getY();
        System.out.println("触发MyListView的触摸事件dispatchTouchEvent");
        getParent().requestDisallowInterceptTouchEvent(true);
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                System.out.println("触发MyListView的ACTION_MOVE事件");
                float deltaX=x-lastX;
                float deltaY=y-lastY;
                if(Math.abs(deltaX)>deltaY){
                    //代表水平滑动
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
                break;
            case MotionEvent.ACTION_UP:
                System.out.println("触发MyListView的ACTION_UP事件");
                break;
        }
        lastX=x;
        lastY=y;
        return super.dispatchTouchEvent(ev);
    }


}
